package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * Author chongge
 * Time 2019/10/28 15:32
 * ClassName com.itheima.service.impl
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {



    @Autowired
    private OrderSettingDao orderSettingDao;
    /***
     * 批量预约设置
     * @param orderSettingList
     */
    @Override
    public void save(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0){
            for (OrderSetting orderSetting : orderSettingList) {
                //根据预约日期先查询数据是否存在
                int count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                //存在，更新数据
                if (count > 0) {
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {

                    //不存在，保存
                    orderSetting.setReservations(0);//已预约数量为0
                    orderSettingDao.save(orderSetting);
                }

            }

        }else {
            System.out.println("数据为空");
        }
    }

    /***
     * 根据年月查询预约数据并回显
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //date:2019-10
        String startDate = date+"-1";
        String endDate = date+"-31";
        Map<String,String> map = new HashMap();
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);

        List<Map> mapList = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            Map<String,Object> data = new HashMap<>();
            data.put("date",orderSetting.getOrderDate().getDate());//获取日期
            data.put("number",orderSetting.getNumber());//获取总数
            data.put("reservations",orderSetting.getReservations());//获取已预约人数
            mapList.add(data);
        }
        System.out.println(mapList);

        return mapList;

    }

    /***
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        int count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else {
            orderSettingDao.save(orderSetting);
        }
    }

}
